import 'package:enjoy_plus_demo/pages/home/index.dart';
import 'package:enjoy_plus_demo/pages/mine/index.dart';
import 'package:enjoy_plus_demo/utils/eventbus.dart';
import 'package:enjoy_plus_demo/utils/toast.dart';
import 'package:enjoy_plus_demo/utils/token.dart';
import 'package:flutter/material.dart';

class TabBarPage extends StatefulWidget {
  const TabBarPage({super.key});

  @override
  State<TabBarPage> createState() => _TabBarPageState();
}

class _TabBarPageState extends State<TabBarPage> {
  //当前选中页面对应的索引
  int currentIndex = 0;
  // 底部TabBar数据
  List tabBarList = [
    {
      "title": "首页",
      "default": "assets/tabs/home_default.png",
      "active": "assets/tabs/home_active.png",
    },
    {
      "title": "我的",
      "default": "assets/tabs/my_default.png",
      "active": "assets/tabs/my_active.png",
    },
  ];

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration.zero, () async {
      await TokenManager().init();
      // ToastUtil.showSuccess(TokenManager().getToken()!);
    });

    //注册跳转到登录页的通知接收
    eventBus.on<LogoutEvent>().listen((event) {
      Navigator.pushNamed(context, '/login');
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body:
          //  SafeArea(
          //   //
          //   child: IndexedStack(
          //     //tabBar的索引值
          //     index: currentIndex,
          //     children: [
          //       //
          //       HomePage(),
          //       MinePage(),
          //     ],
          //   ),
          // ),
          //去除安全区域 ：将33 - 44 行注释 解开46-54的注释即可
          IndexedStack(
        //tabBar的索引值
        index: currentIndex,
        children: [
          //
          HomePage(),
          MinePage(
            currentIndex: currentIndex,
          ),
        ],
      ),
      //底部导航条
      bottomNavigationBar: BottomNavigationBar(
        //设置底部导航item的样式
        type: BottomNavigationBarType.fixed,
        currentIndex: currentIndex,
        items: barItemList(),
        selectedItemColor: Color.fromARGB(255, 85, 145, 175),
        unselectedItemColor: Colors.grey,

        //  [
        //   BottomNavigationBarItem(
        //     icon: Image.asset('images/home_icon@2x.png', width: 25),
        //     label: '首页',
        //   ),
        //   BottomNavigationBarItem(
        //     icon: Image.asset('images/mine_icon@2x.png', width: 25),
        //     label: '我的',
        //   ),
        // ],
        onTap: (event) {
          setState(() {
            currentIndex = event;
          });
        },
      ),
    );
  }
  //提供一个底部导航栏的Item

  List<BottomNavigationBarItem> barItemList() {
    List<BottomNavigationBarItem> tempList = [];

    for (var item in tabBarList) {
      tempList.add(
        BottomNavigationBarItem(
          icon: Image.asset(item['default'], width: 25),
          activeIcon: Image.asset(item['active'], width: 25),
          label: item['title'],
        ),
      );
    }
    return tempList;
  }
}
